package com.fqg.weblucene.web;

import cn.hutool.http.ContentType;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.action.Action;
import cn.hutool.json.JSONUtil;
import com.fqg.weblucene.lucene.utils.LuceneUtils;
import com.fqg.weblucene.web.dto.PageSearch;
import com.fqg.weblucene.web.dto.SearchRequestDTO;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fqg05007 on 2021/9/1.
 */
public class PageSearchAction implements Action {
    @Override
    public void doAction(HttpServerRequest httpServerRequest, HttpServerResponse httpServerResponse) throws IOException {
        PageSearch pageSearch= JSONUtil.toBean(httpServerRequest.getBody(),PageSearch.class);
        Map<String,String> searchKeys=new HashMap<>();
        for(SearchRequestDTO searchRequestDTO:pageSearch.getSearchCoditions()){
                searchKeys.put(searchRequestDTO.getVkey(),searchRequestDTO.getVvalue());
        }
        try {
            ;
            httpServerResponse.write(JSONUtil.toJsonStr(LuceneUtils.searchByConditionPage(searchKeys,pageSearch.getCurrentPage(),pageSearch.getPageSize())), ContentType.JSON.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
